const apiHostUrl = process.env.API_HOST_URL ? process.env.API_HOST_URL : 'http://localhost:8091';
// // 日志打印函数
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const log = (message: string, data?: any) => {
    console.log(`[LOG]: ${message}`, data || '');
};
//装配抽奖
export const strategyArmory = (activityId : number) => {
    const url = `${apiHostUrl}/api/v1/raffle/strategy_armory?activityId=${activityId}`;
    try{
        log('Calling strategyArmory API', { activityId, url });
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/armory?activityId=${activityId}`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            }
        });
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    }catch(err){
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"装配失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}
//
/**
 * 查询抽奖奖品列表
 * @param userId 用户ID
 * @param activityId 用户ID
 */
export const queryRaffleAwardList =  (userId?: string | null, activityId?: number | null) => {
    const url = `${apiHostUrl}/api/v1/raffle/strategy/query_raffle_award_list`;
    try{
        log('Calling queryRaffleAwardList API', { userId,activityId, url });
        return fetch(`${apiHostUrl}/api/v1/raffle/strategy/query_raffle_award_list`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                userId: userId,
                activityId: activityId
            })
        });
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    }catch(err){
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}
export const draw = (userId?: string | null, activityId?: number | null) => {
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/draw`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            body: JSON.stringify({
                userId: userId,
                activityId: activityId
            })
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}


/**
 * 查询账户额度
 * @param userId        用户ID
 * @param activityId    活动ID
 */
export const queryUserActivityAccount =(userId?: string | null, activityId?: number | null)  => {
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/query_user_activity_account`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            body: JSON.stringify({
                userId: userId,
                activityId: activityId
            })
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}

/**
 * 日历签到返利接口
 * @param userId
 */
export const calendarSignRebate = (userId?: string | null) => {
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/calendar_sign_rebate?userId=${userId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}

/**
 * 判断是否签到接口
 * @param userId
 */
export const isCalendarSignRebate = (userId?: string | null) => {
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/is_calendar_sign_rebate?userId=${userId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}

/**
 * 查询权重规则
 * @param userId        用户ID
 * @param activityId    活动ID
 */
export const queryRaffleStrategyRuleWeight = (userId?: string | null, activityId?: number | null)  => {
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/strategy/query_raffle_strategy_rule_weight`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            body: JSON.stringify({
                userId: userId,
                activityId: activityId
            })
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}


export const queryUserCreditAccount = (userId?: string)=>{
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/query_user_credit?userId=${userId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}

export const querySkuProductListByActivityId = (activityId?: number)=>{
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/query_sku_products?activityId=${activityId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}

export const creditPayExchangeSku = (userId?: string, sku?: number) => {
    try {
        return fetch(`${apiHostUrl}/api/v1/raffle/activity/credit_pay_exchange_sku`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            body: JSON.stringify({
                userId: userId,
                sku: sku
            })
        })
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
    } catch (error) {
        return fetch("{\n" +
            "    \"code\": \"0001\",\n" +
            "    \"info\": \"调用失败\",\n" +
            "    \"data\": [\n" +
            "}");
    }
}



